package com.wontube.smr.web.controller.video;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.client.remote.Remoting;
import com.wontube.smr.client.model.po.Genre;
import com.wontube.smr.client.model.po.TVEpisode;
import com.wontube.smr.client.model.po.TVShow;
import com.wontube.smr.client.model.vo.SeasonEpisodes;
import com.wontube.smr.client.model.vo.TVShowItems;
import com.wontube.smr.web.client.ITVWebHandler;
import com.wontube.smr.web.client.model.bo.QueryItemParams;

/**
 * @author luobiao(2012-7-24)
 * 
 */
@Controller
public class TVWebController {
	private final static int CATEGORY_PAGE_SIZE = 30;
	
	@RequestMapping("/tv")
	public void tvHome(HttpServletRequest request, 
			HttpServletResponse response) throws IOException{
		response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		response.sendRedirect(request.getContextPath() + "/tv/");		
	}
	
	@RequestMapping("/tv/")
	public ModelAndView tvHome() {
		return showHome();
	}
	
	@RequestMapping("/tv/show")
	public void f2(HttpServletRequest request, 
			HttpServletResponse response) throws IOException{
		response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		response.sendRedirect(request.getContextPath() + "/tv/full-length/");		
	}
	
	@RequestMapping("/tv/full-length")
	public void t1(HttpServletRequest request, 
			HttpServletResponse response) throws IOException{
		response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
		response.sendRedirect(request.getContextPath() + "/tv/full-length/");		
	}

	/**
	 * tv show 首页
	 * 
	 * @return
	 */
	@RequestMapping("/tv/full-length/")
	public ModelAndView showHome() {
		ITVWebHandler handler = getTvWebHandler();
		ModelAndView mv = new ModelAndView("/video/tv/tv_index");
		QueryItemParams params = new QueryItemParams();
		params.setNeedGenres(true);
		TVShowItems showItems = handler.listHuluTVShows(params, new PagenateInfo(1, CATEGORY_PAGE_SIZE));
		if (showItems != null) {
			mv.addObject("tvGenres", showItems.getTvGenres());
		}
		mv.addObject("huluTVShows", showItems);
		return mv;
	}

	@RequestMapping("/tv/full-lengths")
	public ModelAndView tvHomes() {
		ITVWebHandler handler = getTvWebHandler();
		ModelAndView mv = new ModelAndView("/video/tv/tv_index");
		mv.addObject("tvGenres", handler.listTVGenres());
		return mv;
	}

	/**
	 * tv show 详情页面
	 * 
	 * @param showId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/tv/full-length/{id}")
	public ModelAndView tvShowDetail(@PathVariable("id") String showId, HttpServletResponse response)
			throws IOException {
		ITVWebHandler handler = getTvWebHandler();
		TVShow show = handler.getTVShow(showId);

		if (show == null) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return null;
		}

		// 得到最后一季 最后一集的ID
		List<SeasonEpisodes> seasons = handler.listTVEpisodeDetail(showId);
		List<TVEpisode> temp = seasons.get(seasons.size() - 1).getEpisodes();
		String playid = null;
		String itemUrl = null;
		if (temp.size() > 0) {
			TVEpisode tv = temp.get(temp.size() - 1);
			playid = tv.getId();
			itemUrl = tv.getUrl();
		}

		ModelAndView mv = new ModelAndView("/video/tv/detail/tv_detail_index");
		mv.addObject("show", show);
		mv.addObject("playid", playid);
		mv.addObject("itemUrl", itemUrl);
		mv.addObject("tvGenres", handler.listTVGenres());
		return mv;
	}

	/**
	 * tv 相关的episodes详情列表
	 * 
	 * @param showId
	 * @return
	 */
	@RequestMapping("/tv/full-length/episodes_detail")
	public ModelAndView getShowEpisodes(@RequestParam("showId") String showId) {
		ModelAndView mv = new ModelAndView("/video/tv/detail/tv_episodes_detail");
		List<SeasonEpisodes> seasons = getTVEpisodeList(showId);
		mv.addObject("seasons", seasons);
		return mv;
	}

	/**
	 * tv的episodes播放列表
	 * 
	 * @param showId
	 * @return
	 */
	/*
	 * @RequestMapping("/tv/show/episodes_playlist") public ModelAndView
	 * getShowPlaylist(@RequestParam("showId") String showId){ ModelAndView mv =
	 * new ModelAndView("/video/tv/detail/episodes_playlist");
	 * List<SeasonEpisodes> seasons = getTVEpisodeList(showId); if(seasons !=
	 * null){ if(seasons.size() > 0){ TVEpisode ep =
	 * seasons.get(0).getEpisodes().get(0); if(ep != null){ mv.addObject("vUrl",
	 * ep.getUrl()); } } } mv.addObject("seasons", seasons); return mv; }
	 */

	@RequestMapping("/tv/full-length/watch-{id}")
	public ModelAndView play_episode(@PathVariable("id") String id, HttpServletResponse response) throws IOException {
		ITVWebHandler handler = getTvWebHandler();
		TVEpisode show = handler.getTVEpisode(id);
		List<Genre> genres = handler.listTVGenres();
		if (show == null) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
			return null;
		} else {
			List<SeasonEpisodes> seasons = handler.listTVEpisodeDetail(show.getShowId());
			ModelAndView mv = new ModelAndView("video/tv/play/play_episode");
			mv.addObject("show", show);
			mv.addObject("tvGenres", genres);
			mv.addObject("seasons", seasons);
			return mv;
		}
	}

	/**
	 * tv 各种条件查询
	 * 
	 * @param genre
	 *            decade alpha
	 * @return
	 */
	@RequestMapping("/tv/full-length/query")
	public ModelAndView query(QueryItemParams params,
			@RequestParam(value = "page", required = false, defaultValue = "1") Integer page) {
		ITVWebHandler handler = getTvWebHandler();

		params.setNeedGenres(false);
		TVShowItems tvShowItems = handler.listHuluTVShows(params, new PagenateInfo(page, CATEGORY_PAGE_SIZE));

		ModelAndView mv = new ModelAndView("/video/tv/tv_content");
		mv.addObject("huluTVShows", tvShowItems);
		return mv;
	}

	@RequestMapping("/tv/full-length/genre/{genre}")
	public ModelAndView getShowByGenre(@PathVariable("genre") String genre, HttpServletResponse response)
			throws IOException {
		ModelAndView mv = getTVShowViewWithGenres(genre, null, null);
		mv.addObject("genre",genre);
		return mv;
	}

	@RequestMapping("/tv/full-length/decade/{decade}")
	public ModelAndView getShowByDecade(@PathVariable("decade") Integer decade) throws IOException {
		ModelAndView mv = getTVShowViewWithGenres(null, null, decade);
		mv.addObject("decade",decade);
		return mv;
	}

	@RequestMapping("/tv/full-length/alpha/{alpha}")
	public ModelAndView getShowByinitial(@PathVariable("alpha") String initial) throws IOException {
		ModelAndView mv = getTVShowViewWithGenres(null, initial, null);
		mv.addObject("alpha",initial);
		return mv;
	}

	@RequestMapping("/tv/full-length/relate/{id}")
	public ModelAndView getShowRelate(@PathVariable("id") String id) throws IOException {
		ITVWebHandler handler = getTvWebHandler();	
		
		QueryItemParams params = new QueryItemParams();
		TVShowItems showItems = handler.listHuluTVShows(params, new PagenateInfo(1, 6));		
		List<TVShow> list = new ArrayList<TVShow>();
		for (TVShow tv : showItems.getItems()) {
			if (!tv.getId().equals(id) && list.size() < 5) {
				list.add(tv);
			}
		}
		showItems.setItems(list);
		
		ModelAndView mv = new ModelAndView("/video/tv/play/relate_tv");
		mv.addObject("showItems", showItems);
		return mv;
	}

	private ModelAndView getTVShowViewWithGenres(String genre, String initial, Integer decade) {
		ITVWebHandler handler = getTvWebHandler();
		QueryItemParams params = new QueryItemParams();
		params.setNeedGenres(true);	
		params.setInitial(initial);
		params.setDecade(decade);
		params.setGenre(genre);
		TVShowItems showItems = handler.listHuluTVShows(params, new PagenateInfo(1, CATEGORY_PAGE_SIZE));
		ModelAndView mv = new ModelAndView("/video/tv/tv_index");
		if (showItems != null) {
			mv.addObject("tvGenres", showItems.getTvGenres());
		}
		mv.addObject("huluTVShows", showItems);
		return mv;
	}

	private List<SeasonEpisodes> getTVEpisodeList(String showId) {
		ITVWebHandler handler = getTvWebHandler();
		List<SeasonEpisodes> seasons = handler.listTVEpisodeDetail(showId);
		return seasons;
	}

	private ITVWebHandler getTvWebHandler() {
		Remoting<ITVWebHandler> remoting = new Remoting<ITVWebHandler>();
		return remoting.createService(ITVWebHandler.class);
	}
}
